// app.js
let SocketTask;
let socketPath='ws://192.168.28.104:8088';
let temp = require('./http/axios').ip
let axios = require('./http/axios');
require('./utils/init')
App({
  onLaunch() {
    const that = this;
    //that.websocketEs(socketPath, {})
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    // 检测token如果存在则跳转到主页 没有进入登录页
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId 这样登录每次进来就是这个微信号的
      }
    })
  },
  // 调用一个通用请求 来判定token
  onShow(){
    //this._messageToken()
  },
  // 调用信息检测接口
  _messageToken(){
    let userId = wx.getStorageSync('userid')
    axios
      .custom("Message", "getMessages", userId)
      .then((res) => {
        if (res.code == 200) {
          console.log(res,'测试token')
        } else {
          console.log(res, '数据格式变动')
        }
      })
      .catch((e) => {
        console.log("错误信息", e);
      });
  },
  // 建立websocket
  websocketEs(url, data = {}) {
    const that = this;
    SocketTask = wx.connectSocket({
      url: url,
      header:{
        'content-type': 'application/json'
      },
      success: (res) => {
        console.log('webSocket 连接成功', res);
      },
      fail: (err) => {
        console.log('websocket连接出错', err);
        wx.showToast({
          title: "网络异常"
        })
      }
    })
    that.globalData.SocketTask =SocketTask
  },
  globalData: {
    //SocketTask: SocketTask,
    urlIp:temp
  }
})
